package sng.modules.view.components.city.strategy
{
	import sng.modules.view.components.city.logic.unit.BaseBuilding;
	import sng.utils.BuildingUtil;
	import sng.utils.IsoUtil;
	
	/**
	 *城市整体策略管理 
	 * @author leonliu
	 * 
	 */	
	public class CityStrategyManage
	{
		public function CityStrategyManage()
		{
		}
		
		
		/**
		 *计算标志性建筑所产生的额外增益并赋值对周围的建筑
		 * @param buildingid
		 * 
		 */		
		public static function setAddBonusRate(building:BaseBuilding):void
		{
			//1.根据传入的建筑id,如果是装饰建筑则修改所影响的增益
			//2.如果是普通建筑则修改自己的变动
			
			if (building.vo.buildingTypeId==BuildingUtil.HOUSE_TYPE||building.vo.buildingTypeId==BuildingUtil.BUSINESS_TYPE)
			{
				//相邻的id数组
				var nearbyArray:Array = IsoUtil.getBuildBonusIdArray(building.uVO.x, building.uVO.y, building.vo.rows, building.vo.cols);
				var leng:int = nearbyArray.length;
				var oneBuilding:BaseBuilding;
				for(var i:int;i<leng;i++)
				{
					oneBuilding = building.cityView.getChildByID(String(nearbyArray[i]));
					if (oneBuilding.vo.buildingTypeId==BuildingUtil.DECORATION_TYPE)//装饰性建筑
					{
						building.uVO.totalBonusRate+=oneBuilding.vo.bonusRate;
					}
				}
			}
			else if (building.vo.buildingTypeId==BuildingUtil.DECORATION_TYPE)//装饰性建筑
			{
				//设置周围建筑的空闲与否
				var nearbyArray2:Array = IsoUtil.getBuildBonusIdArray(building.uVO.x, building.uVO.y, building.vo.rows, building.vo.cols);
				var leng2:int = nearbyArray2.length;
				var oneBuilding2:BaseBuilding;
				for(var j:int;j<leng2;j++)
				{
					oneBuilding2 = building.cityView.getChildByID(String(nearbyArray2[j]));
					if (oneBuilding2.vo.buildingTypeId==BuildingUtil.BUSINESS_TYPE || oneBuilding2.vo.buildingTypeId==BuildingUtil.HOUSE_TYPE)//house或商业建筑
					{
						oneBuilding2.uVO.totalBonusRate+=building.vo.bonusRate;
					}
				}
			}
		}
		
		public static function setDelBonusRate(building:BaseBuilding):void
		{
			//1.根据传入的建筑id,如果是装饰建筑则修改所影响的增益
			//2.如果是普通建筑则修改自己的变动
			
			if (building.vo.buildingTypeId==BuildingUtil.HOUSE_TYPE||building.vo.buildingTypeId==BuildingUtil.BUSINESS_TYPE)
			{
				//相邻的id数组
				var nearbyArray:Array = IsoUtil.getBuildBonusIdArray(building.uVO.x, building.uVO.y, building.vo.rows, building.vo.cols);
				var leng:int = nearbyArray.length;
				var oneBuilding:BaseBuilding;
				for(var i:int;i<leng;i++)
				{
					oneBuilding = building.cityView.getChildByID(String(nearbyArray[i]));
					if (oneBuilding.vo.buildingTypeId==BuildingUtil.DECORATION_TYPE)//装饰性建筑
					{
						building.uVO.totalBonusRate-=oneBuilding.vo.bonusRate;
					}
				}
			}
			else if (building.vo.buildingTypeId==BuildingUtil.DECORATION_TYPE)//装饰性建筑
			{
				//设置周围建筑的空闲与否
				var nearbyArray2:Array = IsoUtil.getBuildBonusIdArray(building.uVO.x, building.uVO.y, building.vo.rows, building.vo.cols);
				var leng2:int = nearbyArray2.length;
				var oneBuilding2:BaseBuilding;
				for(var j:int;j<leng2;j++)
				{
					oneBuilding2 = building.cityView.getChildByID(String(nearbyArray2[j]));
					if (oneBuilding2.vo.buildingTypeId==BuildingUtil.BUSINESS_TYPE || oneBuilding2.vo.buildingTypeId==BuildingUtil.HOUSE_TYPE)//house或商业建筑
					{
						oneBuilding2.uVO.totalBonusRate-=building.vo.bonusRate;
					}
				}
			}
		}
		
		/**
		 *设置城市建筑的空闲与否的状态 
		 * 
		 */		
		public static function setIdelBuilds(building:BaseBuilding,blIdel:Boolean):void
		{
			//1.根据传入的建筑id,如果是公路建筑则修改所影响的周围的状态
			if (building.vo.buildingTypeId==BuildingUtil.HOUSE_TYPE||building.vo.buildingTypeId==BuildingUtil.BUSINESS_TYPE||building.vo.buildingTypeId==BuildingUtil.ROAD_TYPE)
			{
				var sideArray:Array = IsoUtil.getBuildSideIdArray(building.uVO.x, building.uVO.y, building.vo.rows, building.vo.cols);
				var leng:int = sideArray.length;
				var oneBuilding:BaseBuilding;
				
				if (building.vo.buildingTypeId==BuildingUtil.ROAD_TYPE)//路
				{
					
					for(var i:int;i<leng;i++)
					{ 
						oneBuilding = building.cityView.getChildByID(String(sideArray[i]));
						if (oneBuilding.vo.buildingTypeId==BuildingUtil.BUSINESS_TYPE || oneBuilding.vo.buildingTypeId==BuildingUtil.HOUSE_TYPE)//house或商业建筑
						{
							if (blIdel)//设置为空闲
							{
								if (getBuildRoadCount(oneBuilding)<=1)//如果影响此建筑的路只有一条，则移动此建筑会被影响到
									oneBuilding.isIdle=blIdel;//设置空闲与否
							}
							else//设置为不空闲
								
							{
								oneBuilding.isIdle=blIdel;//设置空闲与否
							}
						}
					}
					
				}
				else//商业或民房
				{
					if (blIdel)//移动商业建筑前不需要对此建筑做任何设置
					{
						
					}
					else
					{
						for(var j:int;j<leng;j++)
						{
							oneBuilding = building.cityView.getChildByID(String(sideArray[j]));
							if (oneBuilding.vo.buildingTypeId==BuildingUtil.ROAD_TYPE)//旁边有路
							{
								
								building.isIdle=false;//设置空闲与否
								return;
							}
						}
						building.isIdle=true;
					}
				}
				
				
				
			}
			
		}
		
		/**
		 *获取某建筑周围影响他空闲状态的路建筑的个数 
		 * @return 
		 * 
		 */		
		private static function getBuildRoadCount(building:BaseBuilding):uint
		{
			var iRoadCount:uint=0;
			var sideArray:Array = IsoUtil.getBuildSideIdArray(building.uVO.x, building.uVO.y, building.vo.rows, building.vo.cols);
			var leng:int = sideArray.length;
			var oneBuilding:BaseBuilding;
			
			for(var j:int;j<leng;j++)
			{
				oneBuilding = building.cityView.getChildByID(String(sideArray[j]));
				if (oneBuilding.vo.buildingTypeId==BuildingUtil.ROAD_TYPE)//旁边有路
				{
					iRoadCount+=1;
					
				}
			}
			return iRoadCount;
		}
		
		
	}
}